<html>
<head><meta charset="utf-8"><title>Handling `include!` from a proc-macro · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Handling.20.60include!.60.20from.20a.20proc-macro.html">Handling `include!` from a proc-macro</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="212518124"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Handling%20%60include%21%60%20from%20a%20proc-macro/near/212518124" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Handling.20.60include!.60.20from.20a.20proc-macro.html#212518124">(Oct 07 2020 at 06:03)</a>:</h4>
<p>In a proc-macro I'm trying to write, it seems i need to process the expansion result of <code>include!</code>.  However its argument is processed relative to the module file directory instead of the crate directory. Is it possible to mimic its behavior within a proc-macro?</p>



<a name="212535733"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Handling%20%60include%21%60%20from%20a%20proc-macro/near/212535733" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Handling.20.60include!.60.20from.20a.20proc-macro.html#212535733">(Oct 07 2020 at 09:49)</a>:</h4>
<p>On nightly you have <a href="https://doc.rust-lang.org/proc_macro/struct.Span.html#method.source_file">https://doc.rust-lang.org/proc_macro/struct.Span.html#method.source_file</a>, but on stable you'd need a <code>build.rs</code> script to parse the whole source code and pre-process the transformation.</p>
<p>I'll take the chance that we are in a <code>t-compiler</code> stream to mention something that has been on my mind for a while: "enhance" most built-in macros to support a "preprocessor" / callback pattern to simplify chaining with other macros.<br>
An idea of a possible syntax to do that is showcased in (my) <code>::unstrigify</code> crate: <a href="https://docs.rs/unstringify/0.1.1/unstringify/macro.unstringify.html#remarks">https://docs.rs/unstringify/0.1.1/unstringify/macro.unstringify.html#remarks</a></p>
<p>So, here, for instance, it would be nice if <code>include!</code> accepted the following syntax:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="c1">// or `with_include!` to avoid ambiguity:</span>
<span class="n">include</span><span class="o">!</span><span class="p">(</span><span class="kd">let</span><span class="w"> </span><span class="cp">$code</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">include</span><span class="o">!</span><span class="p">(</span><span class="o">..</span><span class="p">.)</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">     </span><span class="c1">// code whereby all occurences of `$code` are replaced by the return value of `include!`</span>
<span class="w">     </span><span class="c1">// For instance:</span>
<span class="w">    </span><span class="n">my_outer_macro</span><span class="o">!</span><span class="p">(</span><span class="cp">$code</span><span class="p">);</span><span class="w"></span>
<span class="p">})</span><span class="w"></span>
</code></pre></div>



<a name="212574195"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Handling%20%60include%21%60%20from%20a%20proc-macro/near/212574195" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Handling.20.60include!.60.20from.20a.20proc-macro.html#212574195">(Oct 07 2020 at 15:19)</a>:</h4>
<p>thanks!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>